package com.deyuanyun.pic.ctrl.util;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.ctrl.domain.EqptCppowerr;
import com.deyuanyun.pic.ctrl.domain.ExinspCpPower;
import com.deyuanyun.pic.ctrl.excelImport.InterferenceMonitoringExcel;
import com.deyuanyun.pic.ctrl.service.CpTestStationExpandService;
import com.deyuanyun.pic.ctrl.service.ExinspChartService;
import com.deyuanyun.pic.ctrl.service.ImportService;

import java.util.Date;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/11/5）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class AcOrDcUtil {

    public static void acAvg(List<InterferenceMonitoringExcel> data, HashMap param) throws ParseException {
        Double max = 0d;
        Double min = 1000000000d;
        Double sum = 0d;
        Double avg = 0d;
        ImportService importService = SpringAppliction.getBean(ImportService.class);

        Double soilResistivity = importService.queryAcSoilResistivity(AutoImport.getBaseId());
        for(InterferenceMonitoringExcel ime:data)
        {
            Double acInterferenceVoltage = Double.parseDouble(ime.getAcInterferenceVoltage());

            //取最大值
            if(max < acInterferenceVoltage)
            {
                max = acInterferenceVoltage;
            }

            //取最小值
            if(min > acInterferenceVoltage)
            {
                min = acInterferenceVoltage;
            }
            sum += acInterferenceVoltage;
        }
        //平均值
        avg = sum/data.size();
        param.put("min",min);
        param.put("max",max);
        param.put("avg",avg);

        Date startDate =  DateUtil.convertStringToDate(data.get(0).getTestTime(),"yyyy/MM/dd HH:mm:ss");
        Date endDate =  DateUtil.convertStringToDate(data.get(data.size()-1).getTestTime(),"yyyy/MM/dd HH:mm:ss");

        //获取开始时间结束时间
        param.put("date",startDate);
        //获取开始时间结束时间
        param.put("startDate",DateUtil.dateToStringTime(startDate));
        param.put("endDate",DateUtil.dateToStringTime(endDate));

        //时间间隔
        param.put("interval",(endDate.getTime()-startDate.getTime()));

        param.put("acCurrentDensity",(8*avg)/(soilResistivity*Math.PI*0.0113));



    }


    public static String dcAvg(List<InterferenceMonitoringExcel> data, HashMap param) throws ParseException {
        ExinspChartService exinspChartService = SpringAppliction.getBean(ExinspChartService.class);
        CpTestStationExpandService cpTestStationExpandService = SpringAppliction.getBean(CpTestStationExpandService.class);
        ImportService importService = SpringAppliction.getBean(ImportService.class);
        String exInspTaskId=AutoImport.getExInspTaskId();
        String testStationId=importService.queryTestStationId("dc",AutoImport.getBaseId());
        EqptCppowerr cppower = cpTestStationExpandService.queryPower(testStationId);
        if(cppower == null)
        {
            return  "导入失败,无法获取测试桩的阴保电源！";
        }

        ExinspCpPower exinspCpPower = exinspChartService.queryExinspCpPower(exInspTaskId,cppower.getId());
        if(exinspCpPower == null)
        {
            return  "无法获取控制电位！";
        }
        Double settingPotential =exinspCpPower.getSettingPotential().doubleValue();

        Double ratio850 = 0d;
        Double ratio800 = 0d;
        Double ratio750 = 0d;
        Double ratio0 = 0d;

        Double ltSettingPotentialAvg = 0d;
        Double ltSettingPotentialNum = 0d;

        Double gtSettingPotentialAvg = 0d;
        Double gtSettingPotentialNum = 0d;

        Double maxSettingPotentialAvg =0d;
        Double minSettingPotentialAvg =1000000000d;

        for(InterferenceMonitoringExcel ime:data)
        {
            Double acInterferenceVoltage = Double.parseDouble(ime.getAcInterferenceVoltage());
            if(acInterferenceVoltage > -850)
            {
                ratio850++;
            }
            if(acInterferenceVoltage > -800)
            {
                ratio800++;
            }
            if(acInterferenceVoltage > -750)
            {
                ratio750++;
            }
            if(acInterferenceVoltage > 0)
            {
                ratio0++;
            }


            if(acInterferenceVoltage > settingPotential)
            {
                ltSettingPotentialAvg += (acInterferenceVoltage - settingPotential);
                ltSettingPotentialNum ++;
            }

            if(acInterferenceVoltage < settingPotential)
            {
                gtSettingPotentialAvg += (settingPotential - acInterferenceVoltage);
                gtSettingPotentialNum ++;
            }

            if(maxSettingPotentialAvg < (acInterferenceVoltage-settingPotential))
            {
                maxSettingPotentialAvg = (acInterferenceVoltage-settingPotential);
            }
            if(minSettingPotentialAvg > (acInterferenceVoltage-settingPotential))
            {
                minSettingPotentialAvg = (acInterferenceVoltage-settingPotential);
            }
        }
        Date startDate =  DateUtil.convertStringToDate(data.get(0).getTestTime(),"yyyy/MM/dd HH:mm:ss");
        Date endDate =  DateUtil.convertStringToDate(data.get(data.size()-1).getTestTime(),"yyyy/MM/dd HH:mm:ss");

        //获取开始时间结束时间
        param.put("date",startDate);
        param.put("startDate",DateUtil.dateToStringTime(startDate));
        param.put("endDate",DateUtil.dateToStringTime(endDate));

        //时间间隔
        param.put("interval",(endDate.getTime()-startDate.getTime()));

        param.put("ratio850",ratio850/data.size()*100.0);
        param.put("ratio800",ratio800/data.size()*100.0);
        param.put("ratio750",ratio750/data.size()*100.0);
        param.put("ratio0",ratio0/data.size()*100.0);

        //正向平均偏移量（mV）
        param.put("positiveMeanOffset",ltSettingPotentialNum == 0 ? 0:ltSettingPotentialAvg/ltSettingPotentialNum);
        //负向平均偏移量（mV）
        param.put("negativeMeanOffset",gtSettingPotentialNum == 0 ? 0:gtSettingPotentialAvg/gtSettingPotentialNum);

        param.put("maxSettingPotentialAvg",maxSettingPotentialAvg);
        param.put("minSettingPotentialAvg",minSettingPotentialAvg);


        return null;

    }


}
